import { getUserInfo } from '@/api/authorization'
import { removeToken } from '@/utils/auth'

export default {
    namespaced: true,
    state: () => ({
        token: '',
        accountNo: '',
        accountId: '',
        userName: ''
    }),
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_ACCOUNT_ID: (state, accountId) => {
            state.accountId = accountId
        },
        SET_ACCOUNT_N0: (state, accountNo) => {
            state.accountNo = accountNo
        },
        SET_USER_NAME: (state, userName) => {
            state.userName = userName
        },
    },
    actions: {
        getUserInfo({ commit }, accountNo) {
            return new Promise((resolve, reject) => {
                getUserInfo(accountNo).then(response => {

                    commit('SET_ACCOUNT_ID', response.id);
                    commit('SET_ACCOUNT_N0', response.accountNo);
                    commit('SET_USER_NAME', response.realName);

                    resolve();

                    console.log('next222');

                }).catch(error => {
                    reject(error)
                })
            })
        },
        logout({ commit }) {
            commit('SET_TOKEN', '');
            commit('SET_USER_NAME', '');
            removeToken();
        }
    },
    getters: {}
}